package com.imy.accessibility.utils;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityManager;
import android.view.accessibility.AccessibilityNodeInfo;

import com.imy.accessibility.CoreService;
import com.imy.accessibility.bean.CustomNode;
import com.imy.common.util.ALog;

import java.util.List;

public class AccessibilityUtils {

  public static final int RESULT_CODE=10001;

  public static boolean checkAccessibilityEnabled(Context context,String s){
    return false;
  }


  public static boolean checkAccessibilityEnabled(Context context) {
    AccessibilityManager mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
    boolean enabled = mAccessibilityManager.isEnabled();
    if (Build.VERSION.SDK_INT >= 26) {
      if (enabled) {
        return true;
      }
    }
    String name = CoreService.class.getName();
    List<AccessibilityServiceInfo> accessibilityServices =
        mAccessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
    for (AccessibilityServiceInfo info : accessibilityServices) {
      String id = info.getId();
      String s = id.replaceAll("/", "");
      ALog.i("name:" + name + "  --：as" + s);
      if (s.contains(name)) {
        return true;
      }
    }
    return false;
  }

  public static void openAccessibility(Activity context) {
    Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
  }

  public static CustomNode copyBaseNode(AccessibilityNodeInfo nodeInfo) {
    CustomNode customNode = new CustomNode();
    Rect rect = new Rect();
    nodeInfo.getBoundsInParent(rect);
    Rect boundsInParent = customNode.mBoundsInParent;
    boundsInParent.left = rect.left;
    boundsInParent.right = rect.right;
    boundsInParent.top = rect.top;
    boundsInParent.bottom = rect.bottom;
    nodeInfo.getBoundsInScreen(rect);
    Rect boundsInScreen = customNode.mBoundsInScreen;
    boundsInScreen.left = rect.left;
    boundsInScreen.right = rect.right;
    boundsInScreen.top = rect.top;
    boundsInScreen.bottom = rect.bottom;

    customNode.mPackageName = nodeInfo.getPackageName();
    customNode.mClassName = nodeInfo.getClassName();
    customNode.mText = nodeInfo.getText();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      customNode.mHintText = nodeInfo.getHintText();
    }
    customNode.mError = nodeInfo.getError();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
      customNode.mPaneTitle = nodeInfo.getPaneTitle();
    }
    customNode.mContentDescription = nodeInfo.getContentDescription();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
      customNode.mTooltipText = nodeInfo.getTooltipText();
    }
    customNode.mViewIdResourceName = nodeInfo.getViewIdResourceName();
    customNode.checkable = nodeInfo.isCheckable();
    customNode.checked = nodeInfo.isChecked();
    customNode.focusable = nodeInfo.isFocusable();
    customNode.focused = nodeInfo.isFocused();
    customNode.selected = nodeInfo.isSelected();
    customNode.clickable = nodeInfo.isClickable();
    customNode.longClickable = nodeInfo.isLongClickable();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
      customNode.contextClickable = nodeInfo.isContextClickable();
    }
    customNode.enabled = nodeInfo.isEnabled();
    customNode.getChildCount = nodeInfo.getChildCount();
    return customNode;
  }



  public static CustomNode copyNode(AccessibilityNodeInfo nodeInfo, CustomNode parent) {
    //    复制值开始
    CustomNode customNode;
    if (parent == null) {
      customNode = copyBaseNode(nodeInfo);
      customNode.getChildCount=nodeInfo.getChildCount();
    } else {
      customNode = parent;
    }

    for (int i = 0; i < nodeInfo.getChildCount(); i++) {
      AccessibilityNodeInfo child = nodeInfo.getChild(i);
      if (child==null){

        continue;
      }
      CustomNode customChild = copyBaseNode(child);
      customChild.parent = customNode;
      int childCount = child.getChildCount();
      customChild.getChildCount = childCount;

      customNode.child.add(customChild);
      if (childCount > 0) {
        copyNode(child, customChild);
      }
    }
    return customNode;
  }

}
